﻿@model IEnumerable<RMS.Models.Problem>
@using RMS.Models
@{
    ViewBag.Title = "Index";
    Company company = (Company)ViewData["company"];
    RMSDataClassesDataContext db = new RMSDataClassesDataContext();
    User user = db.Users.FirstOrDefault(x=>x.UserName == User.Identity.Name);
}

<h2>Index</h2>
@if (Request.IsAuthenticated&&company != null)
{
    <p>
        [ @Html.ActionLink("Create General Problem", "Create", new { companyId = company.Id }) ]
        [ @Html.ActionLink("Create Problem for Product", "Index", "Product") ]
</p>
}

<table>

    <tr>
        <th colspan="8" class="top">
            Shared Problems
        </th>
    </tr>
    <tr>
        <th>
            RVN
        </th>
        <th></th>
        <th>
            Problem Group
        </th>
        <th>
            Risk Type
        </th>
        <th>
            Product
        </th>
        <th>
            Name
        </th>
        <th>
            Approved By
        </th>
        @if (Request.IsAuthenticated && company != null)
        {
            <th>
                Company's problem
            </th>
        }
    </tr>

@foreach (var item in Model.Where(x=>x.IsShared && x.ApprovalUserId.HasValue&&!x.IsDeclined).OrderByDescending(x=>x.AverageRVN)) 
{
    <tr>
        <td>
            @item.AverageRVN
        </td>
        <td>
            @Html.ActionLink("Details", "Details", new { id=item.Id })
        </td>
        <td>
            @item.ProblemGroup.Name
        </td>
        <td>
            @item.RiskType.Name
        </td>
        <td>
            @if (item.ProductId.HasValue)
            {
                @item.Product.Name
            }
            else
            {
                @Html.Encode("General Problem")
            }
          
        </td>
        <td>
            @item.Name
        </td>
        <td>
            @item.ApprovingUser.Name
        </td>
        @if (Request.IsAuthenticated && company != null)
        {
        <td>
            @if (item.CompaniesProblems.Any(x => x.CompanyId == user.CompanyId.Value))
            {
                <text>Yes</text>
            }
            else
            {
                @Html.ActionLink("Add", "AddProblemToCompany",
                    new { companyId = user.CompanyId.Value, problemId = item.Id })
            }
        
        </td>
        }
    </tr>
}

</table>

<br />

@if (Request.IsAuthenticated && company != null)
{
    <table>
    <tr>
        <th colspan="7" class="top">
            Problems for @company.Name
        </th>
    </tr>
    <tr>
        <th>
            RVN
        </th>
        <th></th>
        <th>
            ProblemGroup
        </th>
        <th>
            Risk Type
        </th>
        <th>
            Product
        </th>
        <th>
            Name
        </th>
        <th>
        Status
        </th>
    </tr>

@foreach (var item in Model.Where(x => x.CompaniesProblems.Any(y => y.CompanyId == company.Id)).OrderByDescending(x=>x.CompanyRVN(company.Id)))
{
    <tr>
        <td>
            @item.CompanyRVN(company.Id)
        </td>
        <td>
            @Html.ActionLink("Details", "Details", new { id = item.Id })
        </td>
        <td>
            @item.ProblemGroup.Name
        </td>
        <td>
            @item.RiskType.Name
        </td>
        <td>
            @if (item.ProductId.HasValue)
            {
                @item.Product.Name
            }
            else
            {
                @Html.Encode("General Problem")
            }
          
        </td>
        <td>
            @item.Name
        </td>
        <td>
            @if (item.ApprovalUserId.HasValue && !item.IsDeclined)
            {
                <text>Shared</text>
            }
            else if (item.IsDeclined)
            {
                <text>Declined! [ @item.Reason ]</text>
            }
            else if (item.IsShared)
            {
                <text>Waiting for Approve</text>
            }
            else
            {
                <text>@Html.ActionLink("Request for Approve", "RequestApprove", new { id = item.Id }, null)</text>
            }
        </td>
    </tr>
}

</table>
}
